<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>WebSocket Test</title>
  <script type="text/javascript">
    var socket;

    function openConnection() {
      if ("WebSocket" in window) {
        socket = new WebSocket("ws://127.0.0.1:18023/ChatScope?email=122913027@qq.com&&lastTime=0");
        socket.onopen = function(event) {
          console.log("Connection opened");
          // sendMessage("Hello, WebSocket from browser!");
        };
        socket.onmessage = function(event) {
          console.log("Received from server: " + event.data);
        };
        socket.onclose = function(event) {
          console.log("Connection closed");
        };
        socket.onerror = function(error) {
          console.error("WebSocket error observed:", error);
        };
      } else {
        alert("Your browser does not support WebSocket!");
      }
    }

    function sendMessage(message) {
      if (socket.readyState === WebSocket.OPEN) {
        socket.send(message);
      } else {
        console.log("WebSocket is not open. ReadyState: " + socket.readyState);
      }
    }

    function closeConnection() {
      if (socket !== null) {
        socket.close();
      }
    }
  </script>
</head>
<body>
<h1>WebSocket Test</h1>
<button onclick="openConnection()">Open Connection</button>
<button onclick="sendMessage('Another message from browser')">Send Message</button>
<button onclick="closeConnection()">Close Connection</button>
</body>
</html>